const express = require('express');
const router = express.Router();
const pool = require('../../dao/pool');

// 管理员登录
router.post('/login', (req, res) => {
    let params = [req.body.admin, req.body.password];
    let sql = 'select admin from admin where admin=? and password=?'
    pool.query(sql, params, (err, result) => {
        if (err) throw err;
        if (result.length > 0) {
            res.send(result[0]);
        } else {
            res.send('0');
        }
    })
})

// 获取题目类型
router.get('/getType', (req, res) => {
    let sql = 'select id,type from type';
    pool.query(sql, (err, result) => {
        if (err) throw err
        res.send(result)
    })
})

// 获取用户信息
router.get('/getUser', (req, res) => {
    let sql = 'select id,username,address,integral,phone,email,prize from user';
    pool.query(sql, (err, result) => {
        if (err) throw err
        res.send(result)
    })
})

// 添加题目
router.post('/addSubject', (req, res) => {
    let params = req.body;
    let sql = 'insert into subject set ?'
    pool.query(sql, [params], (err, result) => {
        if (err) throw err
        if (result[0].affectedRows != 0) {
            res.send('1')
        } else {
            res.send('0')
        }
    })
})

// 获取奖品，返回奖品名称，价值，id
router.get('/getPrize', (req, res) => {
    let sql = 'select id,prize,price from prize';
    pool.query(sql, (err, result) => {
        if (err) throw err
        res.send(result)
    })
})

// 添加奖品，成功返回所有奖品信息，失败返回0，需要传值（prize，price）
router.post('/addPrize', (req, res) => {
    let params = req.body;
    let sql = 'insert into prize set ?'
    pool.query(sql, [params], (err, result) => {
        if (err) throw err
        if (result[0].affectedRows != 0) {
            let sql = 'select id,prize,price from prize';
            pool.query(sql, (err, result) => {
                if (err) throw err
                res.send(result)
            })
        } else {
            res.send('0')
        }
    })
})

module.exports = router;